//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by Jet on 16/5/6.
//  Copyright © 2016年 Jet. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelagete:NSObjectProtocol {
   
  func visitorWillLogin ()
    //设置可选协议方法
  optional func visitorWillRegister()

}

class VisitorLoginView: UIView {

    //创建代理属性
    weak var visitorDelegate : VisitorLoginViewDelagete?
    
   @objc func loginBtnDidClick(){
        visitorDelegate?.visitorWillLogin()
    }
    
   @objc func registerBtnDidClick(){
        visitorDelegate?.visitorWillRegister?()
    }
    
    
    init(){
    
        super.init(frame: CGRectZero)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    func setUIInfo(imageName:String?, title:String) {
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            iconView.hidden = true
        }else{
          startAnimation()
        }
        
    }
    
    func startAnimation(){
        

        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = 2*M_PI
        anim.duration = 10;
        anim.repeatCount = MAXFLOAT
        //当动画结束　或者视图处于非活跃状态　动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    
   private func setupUI(){
    
        //backgroundColor = UIColor.orangeColor()
    addSubview(circleView)
    addSubview(backView)
    addSubview(iconView)
    addSubview(tipLabel)
    addSubview(loginBtn)
    addSubview(registerBtn)
  
    //设置背景颜色(这个很重要)
    backgroundColor = UIColor(white: 0.93, alpha: 1)
    
    //设置frame布局失效
    for v in subviews {
        v.translatesAutoresizingMaskIntoConstraints = false
    }
    
    //设置布局VFL,布局是尽量相对一个控件
    //circleView的约束
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
    
    //iconView的约束
    addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
    
    //tipLabel的约束
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
    
    //登录按钮的约束
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
   
    //注册按钮的约束
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
    
    //设置背景视图的约束
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-50)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView,"registerBtn":registerBtn]))
    
    loginBtn.addTarget(self, action: #selector(VisitorLoginView.loginBtnDidClick), forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: #selector(VisitorLoginView.registerBtnDidClick), forControlEvents: .TouchUpInside)
    
    }
    
    // MARK: - 懒加载所以控件
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel:UILabel = {
    
        let label = UILabel()
        label.text = "你立于微风之中一袭长裙白衣似雪惊艳了时光"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    private lazy var loginBtn:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        return btn
    }()
    
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
    }()
    
        
    }
    
    
    

